package com.our.sys.project1.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.our.sys.project1.service.TBusinessService;

@Controller
@RequestMapping("/t_business")
public class TBusinessController {

    @Autowired
    private TBusinessService tBusinessService;

    private static Logger logger = LoggerFactory.getLogger(SupplierController.class);

    @RequestMapping(value = "/query_flow_path")
    public @ResponseBody
    Map<String, Object> queryFlowPath(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String content = tBusinessService.getFlowPathContent();
            map.put("success", true);
            map.put("content", content);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/query_knowledge")
    public @ResponseBody
    Map<String, Object> queryKnowledge(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String mId = request.getParameter("mId");
            String content = tBusinessService.getKnowledgeContent(Long.valueOf(mId));
            map.put("success", true);
            map.put("content", content);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/query_rule")
    public @ResponseBody
    Map<String, Object> queryRule(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String tId = request.getParameter("tId");
            String content = tBusinessService.getRuleContent(Long.valueOf(tId));
            map.put("success", true);
            map.put("content", content);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/save_flow_path")
    public @ResponseBody
    Map<String, Object> saveFlowPath(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String content = request.getParameter("content");
            tBusinessService.saveFlowPath(content);
            map.put("success", true);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/save_knowledge")
    public @ResponseBody
    Map<String, Object> saveKnowledge(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String mId = request.getParameter("mId");
            String content = request.getParameter("content");
            tBusinessService.saveKnowledge(Long.valueOf(mId), content);
            map.put("success", true);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/save_rule")
    public @ResponseBody
    Map<String, Object> saveRule(HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String tId = request.getParameter("tId");
            String content = request.getParameter("content");
            tBusinessService.saveRule(Long.valueOf(tId), content);
            map.put("success", true);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }
    
    @RequestMapping(value = "/delete_flow_path")
    public @ResponseBody
    Map<String, Object> deleteFlowPath() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            tBusinessService.deleteFlowPath();
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }
    
    @RequestMapping(value = "/delete_knowledge")
    public @ResponseBody
    Map<String, Object> deleteKnowledge(@RequestParam("id") String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            tBusinessService.deleteKnowledge(Long.valueOf(id));
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }
    
    @RequestMapping(value = "/delete_rule")
    public @ResponseBody
    Map<String, Object> deleteRule(@RequestParam("id") String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            tBusinessService.deleteRule(Long.valueOf(id));
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }
}
